package com.sanweibook.jhook.web.sys;

import com.google.common.collect.Maps;
import com.sanweibook.jhook.common.tool.excel.ExcelHandler;
import com.sanweibook.jhook.common.util.FileHandlerUtils;
import freemarker.template.TemplateException;
import net.sf.excelutils.ExcelException;
import net.sf.excelutils.ExcelUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by twg on 2017/5/27.
 */
@RequestMapping("/admin/fileHandler")
@Controller
public class FileHandlerController {

    @RequestMapping("download")
    public void downloadFile(HttpServletResponse response) throws IOException, TemplateException {
        String burlPath = "http://uploads.yjbys.com/allimg/150608/3-15060P9230a31.jpg";
        String zurlPath = "http://uploads.yjbys.com/allimg/150608/3-15060P92211Z4.jpg";
        String b = FileHandlerUtils.convertFileToBase64( burlPath );//反面
        String z = FileHandlerUtils.convertFileToBase64( zurlPath );//正面

        String filePath = FileHandlerUtils.generateFilePath( "word" );

        Map<String, String> stringMap = Maps.newHashMap();
        stringMap.put( "card1", b );
        stringMap.put( "card2", z );
        stringMap.put( "card3", z );
        stringMap.put( "card4", b );
        FileHandlerUtils.generateFile( stringMap, filePath, "特殊DOC" + System.currentTimeMillis() + ".doc" );

        FileHandlerUtils.downloadZipFile( filePath, "身份证DOC" + System.currentTimeMillis() + ".zip",response);

    }

    @RequestMapping("download/excel")
    public void downloadExcelFile(HttpServletRequest request,HttpServletResponse response){


        Map context = new HashMap();
        context.put("tranType", "201");
        context.put("index", new Integer(1));
        context.put("key", "key0");
        context.put("dd", "Date");
        context.put("patten", "yyyy-MM-dd");
        context.put("width", "2");
        context.put("width1", new Integer(11));
        context.put("array_int", new int[] {22,33,44});

        Map context1 = new HashMap();
        context1.put("tranType", "203");
        context1.put("index", new Integer(1));
        context1.put("key", "key0");
        context1.put("dd", "Date");
        context1.put("patten", "yyyy-MM-dd");
        context1.put("width", "2");
        context1.put("width1", new Integer(11));
        context1.put("array", new String[] {"111","222","333"});
        context1.put("array_int", new int[] {22,33,44});

        List lists = new ArrayList();
        lists.add(context);

        lists.add(context1);


        ExcelUtils.addValue("lists", lists);

//        response.reset();
//        response.setContentType("application/vnd.ms-excel");
//        response.setHeader("Content-Disposition", "attachment; filename=\"" + System.currentTimeMillis() + ".xls\"");
        String config = "/excel/mytest.xls";
        OutputStream outputStream  = null;
        try {
            outputStream  = response.getOutputStream();
            ExcelUtils.write(response, "test.xls");
            ExcelUtils.addValue("list", lists);
//            ExcelUtils.addService("webutil", WebUtil.class);
            ExcelUtils.export(request.getSession().getServletContext(),config, response.getOutputStream());//按excel模板导出
        } catch (ExcelException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (outputStream != null){
                try {
                    outputStream.flush();
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}
